package test.sort;

import com.alibaba.rocketmq.shade.com.alibaba.fastjson.JSON;
import test.sort.common.CommonUtil;
import test.sort.pyramidsort.SortAsc;

import java.io.IOException;
import java.util.Arrays;
import java.util.Date;

public class Stage {
    static int x=10000;
    static int y=1000;
//    static int z=100;

    static int length=100000000;

    public static void main(String[] args) throws IOException {
        Stage stage = new Stage();
        /**
         * 生成数组花费时间：2541
         * 单线程归并排序花费时间：18084
         * 是否升序：true
         * Fork/Join归并排序花费时间：12234
         * 是否升序：true
         */
//        stage.test1();
        stage.test2();
//        stage.test3();
//        stage.test4();
//        stage.test5();
    }


    public void test5(){

        int[] intArr = new int[10];
        for (int j = 0; j < 10; j++) {
            intArr[j] = new Double(Math.random() * 10).intValue();
        }
        intArr=new int[]{9,18,26,32,54,67,73,77,83,98};

        SortAsc sortAsc=new SortAsc(intArr);
        sortAsc.sort();
        intArr = new int[10];
        for (int j = 0; j < 10; j++) {
            intArr[j] = new Double(Math.random() * 10).intValue();
        }

        intArr=new int[]{0,1,12,15,26,31,40,43,52,56};

        SortAsc sortAsc2=new SortAsc(intArr);
        sortAsc2.sort();

        sortAsc.exchangeSort(sortAsc2);
    }

    public void test4(){

        int[] intArr = new int[10];
        for (int j = 0; j < 10; j++) {
            intArr[j] = new Double(Math.random() * 10).intValue();
        }
        intArr=new int[]{1,2,3,4,5,6,7,8,9,11};
        SortAsc sortAsc=new SortAsc(intArr);
        sortAsc.sort();
        System.out.println(JSON.toJSONString(intArr));
        for (int i = 11; i >-1; i--) {
            if(i<sortAsc.getMaxvalue()){
                int swap = sortAsc.swapMaxValue(i);
                System.out.println("swapMaxValue="+swap );
                System.out.println(JSON.toJSONString(intArr));
            }
        }
    }

    public void test3(){
        int[] intArr = new int[10];
        for (int j = 0; j < 10; j++) {
            intArr[j] = new Double(Math.random() * 10).intValue();
        }

        System.out.println(JSON.toJSONString(intArr));
        sort(intArr,0,intArr.length-1);
        System.out.println(JSON.toJSONString(intArr));
    }

    private static void sort(int[] a, int left, int right) {
        for (int i = left, j = i; i < right; j = ++i) {
            int ai = a[i + 1];
            System.out.println("ai="+ai);
            while (ai < a[j]) {
                System.out.println("ai="+ai+",a[j]="+a[j]);
                a[j + 1] = a[j];
                if (j-- == left) {
                    break;
                }
            }
            System.out.println("ai="+ai);
            System.out.println(JSON.toJSONString(a));
            a[j + 1] = ai;
            System.out.println(JSON.toJSONString(a));
        }
    }



    public void test2() throws IOException {

        SortAsc root=new SortAsc();
        Stage.x=10000;
        Stage.y=1000;
        //生成数组花费时间：1150032  19分钟
        //不需要排序，按顺序输出就好
        Stage.x=10000;
        Stage.y=1000;
        Stage.length=Stage.x*Stage.y;

        for (int i = 0; i < Stage.x; i++) {
//            SortAsc childx=new SortAsc();
            int[] intArr = new int[y];
            for (int j = 0; j < Stage.y; j++) {
                intArr[j] = new Double(Math.random() * length).intValue();
            }
            SortAsc childy=new SortAsc(intArr);
//            childx.addChild(childy);
            root.addChild(childy);
        }

        // 变量定义
        long begintime = 0;
        long endtime = 0;
        FileUtils fileUtils=new FileUtils("/tmp/win/asus1.txt");
        root.out(fileUtils.getOut(),"\n");
        fileUtils.close();

        fileUtils=new FileUtils("/tmp/win/asus.txt");
        System.out.println("开始排序");


        begintime = new Date().getTime();

        root.childOver();
       while (root.hasSort()){
           root.sort();
       }
        endtime = new Date().getTime();
        root.out(fileUtils.getOut(),"\n");
        fileUtils.close();
        System.out.println("生成数组花费时间：" + (endtime - begintime));

    }

    public void test1() throws IOException {


        // 变量定义
        long begintime = 0;
        long endtime = 0;
        FileUtils fileUtils=new FileUtils("/tmp/win/aa.txt");

        begintime = new Date().getTime();
        // 生成排序数据
        int[] rawArr = generateIntArray(length);
        int[] rawArr2 = Arrays.copyOf(rawArr, rawArr.length);

        endtime = new Date().getTime();
        fileUtils.write(rawArr);
        fileUtils.close();
        System.out.println("生成数组花费时间：" + (endtime - begintime));

        begintime = new Date().getTime();
        new SingleThreadMergeSort().sort(rawArr);


        //System.out.println(Arrays.toString(new SingleThreadMergeSort().sort(rawArr)));
        endtime = new Date().getTime();
        System.out.println("单线程归并排序花费时间：" + (endtime - begintime));
        System.out.println("是否升序："+ CommonUtil.isSorted(rawArr, true));


        fileUtils=new FileUtils("/tmp/win/aa01.txt");
        fileUtils.write(rawArr);
        fileUtils.close();
        System.gc();

        begintime = new Date().getTime();
        new ForkJoinMergeSort().sort(rawArr2);
        //System.out.println(Arrays.toString(new ForkJoinMergeSort().sort(rawArr2)));
        endtime = new Date().getTime();
        System.out.println("Fork/Join归并排序花费时间：" + (endtime - begintime));
        System.out.println("是否升序："+CommonUtil.isSorted(rawArr2, true));

        fileUtils=new FileUtils("/tmp/win/aa02.txt");
        fileUtils.write(rawArr2);
        fileUtils.close();
    }

    /**
     * 生成int类型的数组
     *
     * @return
     */
    private static int[] generateIntArray(int length) {
        int[] intArr = new int[length];
        for (int i = 0; i < length; i++) {
            intArr[i] = new Double(Math.random() * length).intValue();
        }
        return intArr;
    }
}
